import flash.display.Sprite;
import flash.display.Shape;
import TileModel;
import flash.events.Event;
import flash.geom.Rectangle;

class HitAreaView extends Sprite
{
  public var model:TileModel;
  public var camera:Rectangle;
  private var Rect:Shape;
  public var HitType:String;
  public function new(inModel:TileModel,inCamera:Rectangle)
  {
    super();
    this.x = 700; this.y=10;
    model = inModel;
    camera = inCamera;
    model.addEventListener(Event.CHANGE, changeHandler);
    addEventListener(Event.ADDED_TO_STAGE, addedToStagHandler);
    HitType = "HitBox";
    
  }
  private function addedToStagHandler(event:Event)
  {
    //Draw the object
    draw();
    
    //Add listeners
    addEventListener
      (Event.REMOVED_FROM_STAGE, removedFromStageHandler);
  }

  private function removedFromStageHandler(event:Event)
  {
    removeEventListener
      (Event.ADDED_TO_STAGE, addedToStagHandler);
    removeEventListener
      (Event.REMOVED_FROM_STAGE, removedFromStageHandler);
  }
  
  private function changeHandler(event:Event)
  {

    var hitBox = null;
    if(HitType == "AttackBox")
    {
      hitBox = model.GetAttackBox();
    }
    else
    {
      hitBox = model.GetHitBox();
    }
    
    removeChild(Rect);
    Rect = new Shape();
    if(hitBox !=null)
    {
    Rect.graphics.beginFill(0xFFFFFF,0);
    Rect.graphics.lineStyle(1,0x000000);
    Rect.graphics.drawRect(0, 0,  hitBox.width, hitBox.height);

    Rect.graphics.endFill();
    this.x = hitBox.xPos-camera.x; this.y = hitBox.yPos-camera.y;
    }
    addChild(Rect);
  }
  private function draw()
  {
    var hitBox = model.GetHitBox();
    Rect  = new Shape();

    Rect.graphics.beginFill(0xFFFFFF,0);
    Rect.graphics.drawRect(0, 0,  hitBox.width, hitBox.height);
    Rect.graphics.lineStyle(1,0x000000);
    Rect.graphics.endFill();
    addChild(Rect);
    this.x = hitBox.xPos-camera.x; this.y = hitBox.yPos-camera.y;
  }
}